using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.BizTalk.BaseFunctoids;
using System.Reflection;

namespace eliasen.eu.BizTalk.Functoids
{
    public class CarriageReturnLineFeed : BaseFunctoid
    {
        public CarriageReturnLineFeed()
            : base()
		{
			//ID for this functoid
            this.ID = 6424;

			// resource assembly must be ProjectName.ResourceName if building with VS.Net
            SetupResourceAssembly("eliasen.eu.BizTalk.Functoids.eliasen.eu.BizTalk.Functoids.Resources", Assembly.GetExecutingAssembly());

			//Setup the Name, ToolTip, Help Description, and the Bitmap for this functoid
            SetName("Str_CarriageReturnLineFeed_Name");
            SetTooltip("Str_CarriageReturnLineFeed_Tooltip");
            SetDescription("Str_CarriageReturnLineFeed_Description");
            SetBitmap("Bmp_CarriageReturnLineFeed_Icon");

			this.SetMinParams(4);
			this.SetMaxParams(4);

			//set the function name that needs to be called when this Functoid is invoked. This means that
			//this Functoid assembly need to be present in GAC for its availability during Test..Map and Runtime.
            SetExternalFunctionName(GetType().Assembly.FullName, "eliasen.eu.BizTalk.Functoids.CarriageReturnLineFeed", "AddCRLF");

			//category for this functoid. This functoid goes under the String Functoid Tab in the
			//VS.Net toolbox for functoids.
			this.Category = FunctoidCategory.String;
			this.OutputConnectionType = ConnectionType.AllExceptRecord;

			AddInputConnectionType(ConnectionType.AllExceptRecord); //first input
            AddInputConnectionType(ConnectionType.AllExceptRecord); //second input
            AddInputConnectionType(ConnectionType.AllExceptRecord); //third input
            AddInputConnectionType(ConnectionType.AllExceptRecord); //fourth input
		}

		//this is the function that gets called when the Map is executed which has this functoid.
		public string AddCRLF(string str, string addCR, string addLF, string checkFirst)
		{
            string returnStr = str;
            if (str != null)
            {
                bool b_AddCR = Utility.ConvertStringToBool(addCR, false);
                bool b_AddLF = Utility.ConvertStringToBool(addLF, false);

                if (b_AddLF || b_AddCR)
                {
                    bool b_checkfirst = Utility.ConvertStringToBool(checkFirst, false);
                    string strAdd = (b_AddCR ? "\r" : "") + (b_AddLF ? "\n" : "");

                    bool add = !((b_checkfirst) && (returnStr.EndsWith(strAdd)));

                    if (add)
                        returnStr += strAdd;
                }
            }
            else
                returnStr = "\r\n";

            return returnStr;
		}
    }
}
